package org.yjc.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.VerifyCodeUtils;
import org.yjc.entity.Log;
import org.yjc.entity.SysUser;
import org.yjc.repository.SysUserRepository;
import org.yjc.security.UserUtils;
import org.yjc.service.LogService;

@Controller
public class MainController{
	@Autowired
	private UserUtils userutils;
	@Autowired
	private LogService logservice;
	@Autowired
	private SysUserRepository userDAO;
	//发送者的邮箱账号
	@Value("${spring.mail.username}")
	private String mailusername;
	@Autowired
	JavaMailSender jms;

	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	@RequestMapping(value="/")
	public String main() {
		SysUser user = userutils.getUser();	 //获取当前用户
		Log log = new Log();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		log.setName(user.getUsername());
		log.setOperationTime(df.format(new Date())); //获取当前系统时间
		log.setOperatiobName("登入");
		logservice.save(log);
		return "redirect:/main1";
	}
	@RequestMapping(value="/main1")
	public String main1(){
		return "main";
	}
	
	@RequestMapping("/logout1")
	public String logout1() {
		SysUser user = userutils.getUser();	 //获取当前用户
		Log log = new Log();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		log.setName(user.getUsername());
		log.setOperationTime(df.format(new Date())); //获取当前系统时间
		log.setOperatiobName("登出");
		logservice.save(log);
		return "redirect:/logout";
	}
	@RequestMapping("/logout")
	public String logout() {
		return "logout";
	}
	
	@RequestMapping(value="/findpwd")
	public String findpwd() {
		return "findpwd/findpwd";		
	}
	
	@RequestMapping(value="/yx")
	@ResponseBody
	public String yx(String username) {
		//建立邮件消息
		SimpleMailMessage mainMessage = new SimpleMailMessage();
		//发送者
		mainMessage.setFrom(mailusername);
		//接收者
		mainMessage.setTo(username);
		//发送的标题
		mainMessage.setSubject("找回密码验证码");
		String code = VerifyCodeUtils.generateVerifyCode(6);
		//发送的内容
		mainMessage.setText("请记住验证码："+code);
		jms.send(mainMessage);
		System.out.println(code);
		return code;
	}
	
	@RequestMapping(value="/username")
	@ResponseBody
	public Boolean username(String username) {
		SysUser user = userDAO.findByUsername(username);
		//判断数据库中是否存在该用户
		if(user == null) {
			return false;
		}else {
			return true;
		}
	}
	
	@RequestMapping(value="/zhmm1")
	@ResponseBody
	public String zhmm1(String username, String password) {
		SysUser user = userDAO.findByUsername(username); //用username查找用户
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();  //密码加密器
		user.setPassword(encoder.encode(password)); //存入新密码
		userDAO.save(user);
		return "密码找回成功！";
	}
}
